/**
 * START - ONLOAD - JS
 */
/* ----------------------------------------------- */
/* ------------- FrontEnd Functions -------------- */
/* ----------------------------------------------- */

/**
 * runMainSlider: init MAIN SLIDER in HOME
 * @param  {[type]} mslider [description]
 * @return {[type]}         [description]
 */
function runMainSlider(mslider) {
    if (!$(mslider).length) {return;} 

    $(mslider).flexslider();
}

/**
 * [runCalendarIpt description]
 * @param  {[type]} tgt [description]
 * @return {[type]}     [description]
 */
function runCalendarIpt (tgt) {
    if (!$('#' + tgt).length) {return;} 
    new JsDatePick({
        style: {
            top: '37px'
        },
        useMode:2,
        target: tgt,
        dateFormat:"%d-%m-%Y"
    });
}

/**
 * [showSubMenu description]
 * @param  {[type]} mmenu [description]
 * @return {[type]}       [description]
 */
function showSubMenu (mmenu) {
    if (!$(mmenu).length) {return;} 
    if (!$(mmenu).find('.has-child').length) {return;} 

    $(mmenu).find('.has-child').each(function(e) {
        $(this).on('mouseover', function (i) { 
            $(this).find('.sub-menu' + '.' + $(this).data('lvl')).addClass('show');
        }).on('mouseout', function (i) {
            $(this).find('.sub-menu' + '.' + $(this).data('lvl')).removeClass('show');
        });
    });    
}

/**
 * [showMBMenu description]
 * @param  {[type]} mb_menu [description]
 */
function showMBMenu (mb_menu) {
    if (!$(mb_menu).length) {return;} 
    // body...
    $(mb_menu).on('click', function (e) {        
        $this = $(this);
        if (!$($this.data('tgr')).length) {return;} 

        $mb = $($this.data('tgr'));

        if ($mb.hasClass('show')) {
            $this.removeClass('active');
            $mb.removeClass('show');
        } else {
            $this.addClass('active');
            $mb.addClass('show');
        }       
    });
    
    // CLICK OUT
    $( $(mb_menu).data('tgr') ).on( 'clickoutside', function(event){        
        if ($(event.target).hasClass('mb-menu')) {    
            return;
        }
        if ($($(mb_menu).data('tgr')).hasClass('show')) {
            $($(mb_menu).data('tgr')).removeClass('show');
            $(mb_menu).removeClass('active');
        }  
    });
}
/* ----------------------------------------------- */
/* ----------------------------------------------- */
/* OnLoad Page */
jQuery(document).ready(function($){
    showSubMenu('.main-menu.pc-show .mn-menu');
    
    handle_screen_size();
    
    window.onresize = function(event) {
        handle_screen_size();
    };


    function handle_screen_size () {
        if ( document.body.clientWidth <= 960 && $('body').hasClass('grid') ) {
        $('body').removeClass('grid');
    }
    }
});
/* OnLoad Window */
var init = function () {
    runMainSlider('.main-slider .flexslider');
    runMainSlider('.wg-slider .flexslider');
    runCalendarIpt('c-s');
    runCalendarIpt('c-e');
    showMBMenu('.mb-menu');    
};
window.onload = init;